package com.luxx.controller;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;

import static java.lang.Thread.sleep;

@Component
public class AsyncHelper {
    // ...
    @Async
    public void streaming(ResponseBodyEmitter emitter, long eventNumber, long intervalSec) throws IOException, InterruptedException {
        System.out.println("Start Async processing.");
        for (long i = 1; i <= eventNumber; i++) {
            sleep(intervalSec);
            emitter.send("msg" + i + "\n\n");
        }

        emitter.complete();

        System.out.println("End Async processing.");
    }
    // ...
}
